package com.hdey.controller.heng;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdey.pojo.heng.TbUscRole;
import com.hdey.service.heng.RoleService;
import com.hdey.service.heng.UserService;
import com.hdey.utils.Result;
import com.hdey.vo.TreeNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private UserService userService;

    private String msg;

    /**
     * 查询全部结合分页以及模糊查询
     * @param page
     * @param limit
     * @param roleName  角色名称
     * @return
     */
    @GetMapping("/search")
    @ResponseBody
    public Result search(Integer page, Integer limit, String roleName){
        Page<TbUscRole> list=roleService.select(page,limit,roleName);
        return Result.success(list);
    }

    @PostMapping("/selectbyID")
    public Result selectbyID(Integer roleId){
        TbUscRole role=roleService.selectById(roleId);
        return Result.success(role);
    }

    @PostMapping("/update")
    public Result update(TbUscRole tbUscRole){
        try {
            roleService.update(tbUscRole);
            msg="修改成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="状态修改失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }

    @PostMapping("/searchTreeData")
    public Result searchTreeData(Integer id){
        List<TreeNode> list=roleService.searchTreeData(id);
        return Result.success(list);
    }

    @PostMapping("/grant")
    public Result grant(Integer roleId,Integer[] ids){
        System.out.println(ids);
        try {
            roleService.grant(roleId,ids);
            msg="授权成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="授权失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }

    @PostMapping("/add")
    public Result add(TbUscRole tbUscRole){
        System.out.println(tbUscRole);
        try {
            roleService.add(tbUscRole);
            msg="角色添加成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="角色添加失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }

    @PostMapping("/getroleCode")
    public String getroleCode(){
        String no = roleService.getLastroleCode();
        System.out.println(no);
        int num1=Integer.parseInt(no);
        num1++;
        String str = String.format("%04d", num1);//如果小于6位左边补0
        System.out.println(str);
        return str;
    }

    @PostMapping("/del")
    public Result del(Integer roleId){
        try {
            roleService.del(roleId);
            msg="角色删除成功";
            return Result.success(msg);
        }catch (Exception e){
            msg="角色删除失败";
            log.error(msg,e);
            return Result.failed(msg);
        }
    }

    @PostMapping("/selectCoutUser")
    public Integer selectCoutUser(Integer roleId){
        Integer count=userService.selectCoutUser(roleId);
        return count;
    }
}
